package com.ddm.utils;


import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class BeanCopyUtil {

    public static <T> T BeanCopy(Object source , Class<T> clazz) {
        T t = null;
        try {
            t = clazz.getDeclaredConstructor().newInstance();
            BeanUtils.copyProperties(source, t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <O,T> List<T> BeanCopyList(List<O> list , Class<T> clazz){
//        ArrayList<T> resultList = new ArrayList<>();
//        T result = null;
//        try {
//
//            for (O o : list) {
//                result = clazz.getDeclaredConstructor().newInstance();
//                BeanUtils.copyProperties(o , result);
//                resultList.add(result);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return  resultList;

        return list.stream().map(o -> BeanCopy(o , clazz)).collect(Collectors.toList());
    }



}
